{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "x=np.linspace(-np.pi,np.pi,200)  #生成-pi到pi之间的200个值\n",
    "S=np.sin(x)\n",
    "C=np.cos(x)\n",
    "plt.plot(x,S,'ro:')\n",
    "plt.plot(x,C,color='green',linewidth=2.0,linestyle=':')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.xlim(x.min()*2,x.max()*2) #设置坐标轴的长度\n",
    "plt.ylim(C.min()*2,C.max()*2) #设置y轴的长度\n",
    "plt.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],['-PI','-PI/2','0','PI/2','PI'])  #设置x轴的刻度，以及显示的值\n",
    "plt.yticks([-1,-0.5,0,0.5,1])\n",
    "plt.ylabel('y')  #给y轴设置标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax=plt.gca()#get current axis 获取当前坐标轴\n",
    "ax.spines['top'].set_color('none') #隐藏上边的轴\n",
    "ax.spines['right'].set_color('none')  #隐藏右边的轴\n",
    "plt.xlim(-5,5)\n",
    "plt.ylim(-5,5)\n",
    "ax.spines['bottom'].set_position(('data',0))  #把下面的轴移到0的位置\n",
    "ax.spines['left'].set_position(('data',0))  #把下面的轴移到0的位置\n",
    "plt.plot(x,C)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(x,S,label='y=Sin(x)')  #为曲线设置标签\n",
    "plt.plot(x,C,label='y=Cos(x)')\n",
    "plt.legend(loc='upper left')  #显示曲线标签，loc是标签的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(num='sin',figsize=(16,4))  #创建标识符为sin图形，大小是16*4 英寸\n",
    "plt.plot(x,S)  #在sin图形上画线\n",
    "plt.figure(num='cos',figsize=(16,4))  #创建标识符为cos图形，大小是16*4 英寸\n",
    "plt.plot(x,C)  #在cos图形上画线\n",
    "plt.figure(num='sin')  #重新激活标识符为sin的图形，在它上面继续画\n",
    "plt.plot(x,C)\n",
    "print(plt.figure(num='sin').number)  #查看图形的编号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#在一个figure上画多个子图\n",
    "plt.subplot(2,2,1)  #图形分成2*2的区域，取标号为1的区域来作画\n",
    "plt.plot(x,S,label='y=sin(x)')\n",
    "plt.legend()\n",
    "plt.subplot(2,2,2)\n",
    "plt.plot(x,C,label='y=cos(x)')\n",
    "plt.legend()\n",
    "plt.subplot(223)  #(2,2,3)  #取2*2区域中标号为3的区域来作画\n",
    "plt.plot(x,x,label='y=2x')\n",
    "plt.legend()\n",
    "plt.subplot(224)\n",
    "#在标号为4的区域写文字，开始坐标是（0.5,0.5），ha和va是对齐方式，size是文字大小，alpha是透明度\n",
    "plt.text(0.5,0.5,'subplot 4',ha='center',va='center',size=20,alpha=0.5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.gridspec as gridspec\n",
    "plt.figure(figsize=(18,4))\n",
    "G=gridspec.GridSpec(3,3)  #将图形划分成3行3列\n",
    "axes_1=plt.subplot(G[0,:])  #占用第一行所有列\n",
    "plt.xticks(())\n",
    "plt.yticks(())  #清除y轴的刻度\n",
    "plt.text(0.5,0.5,'Axes 1',ha='center',va='center',size=20,alpha=0.5)\n",
    "\n",
    "axes_2=plt.subplot(G[1:,0]) #占用第二行之后所有行，第一列\n",
    "plt.xticks(())\n",
    "plt.yticks(())  #清除y轴的刻度\n",
    "plt.text(0.5,0.5,'Axes 2',ha='center',va='center',size=20,alpha=0.5)\n",
    "\n",
    "axes_3=plt.subplot(G[1:,-1]) #占用第二行之后所有行，最后一列\n",
    "plt.xticks(())\n",
    "plt.yticks(())  #清除y轴的刻度\n",
    "plt.text(0.5,0.5,'Axes 3',ha='center',va='center',size=20,alpha=0.5)\n",
    "\n",
    "axes_4=plt.subplot(G[1,-2]) #占用第二行，倒数第三列\n",
    "plt.xticks(())\n",
    "plt.yticks(())  #清除y轴的刻度\n",
    "plt.text(0.5,0.5,'Axes 4',ha='center',va='center',size=20,alpha=0.5)\n",
    "\n",
    "axes_5=plt.subplot(G[-1,-2]) #占用第二行，倒数第三列\n",
    "plt.xticks(())\n",
    "plt.yticks(())  #清除y轴的刻度\n",
    "plt.text(0.5,0.5,'Axes 5',ha='center',va='center',size=20,alpha=0.5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#散点图\n",
    "n=1024\n",
    "X=np.random.normal(0,1,n)  #随机生成1024个数\n",
    "Y=np.random.normal(0,1,n)\n",
    "C=np.arctan2(Y,X)  #反正切，也就是随机生成1024个颜色的值\n",
    "plt.scatter(X,Y,c=C,s=60,alpha=0.75) #s是点的大小，c是点的颜色，alpha是点的透明度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#柱状图\n",
    "n=12\n",
    "X=np.arange(n)  #X=[0,1,2,3,4,5,6,7,8,9,10,11]\n",
    "Y=(1-X/float(n))*np.random.uniform(0.5,1.0,n) # Y是12个随机数\n",
    "plt.bar(X,Y,facecolor='red',edgecolor='black')  #画柱状图，facecolor是柱体的颜色，edgecolor是边框的颜色\n",
    "for x,y in zip(X,Y):  #zip将X和Y中的每一个值两两配对，用来确定要写的文字的坐标\n",
    "    plt.text(x+0.1,y+0.01,'%.2f'%y,ha='center',va='bottom')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
